package com.liefox.serviceedu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liefox.commonutils.exceptionhandler.GuliException;
import com.liefox.serviceedu.entity.EduSubject;
import com.liefox.serviceedu.entity.excel.SubjectData;
import com.liefox.serviceedu.entity.subject.OneSubject;
import com.liefox.serviceedu.entity.subject.TwoSubject;
import com.liefox.serviceedu.listener.SubjectExcelListener;
import com.liefox.serviceedu.mapper.EduSubjectMapper;
import com.liefox.serviceedu.service.EduSubjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author liefox
 * @since 2021-07-13
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
    @Override
    public void importSubjectData(MultipartFile file, EduSubjectService subjectService) {
        try {
            InputStream in = file.getInputStream();
            // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
            EasyExcel.read(in, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20002, "添加课程分类失败");
        }
    }

    //课程分类树形
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //拿到一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", "0");
        List<EduSubject> oneSubjectsList = baseMapper.selectList(wrapperOne);
        //拿到二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", "0");
        List<EduSubject> twoSubjectsList = baseMapper.selectList(wrapperTwo);
        //创建list集合，用于存储最终封装数据
        List<OneSubject> finalSubjectList = new ArrayList<>();
        //封装一级分类
        /**
         * 查询出来的所有的一级分类list集合遍历，得到每个一级分类对象，获取每个一级分类对象值
         * 封装到要求的list集合里面List《oneSubject》 finalSubject
         * */
        for (int i = 0; i < oneSubjectsList.size(); i++) {
            //得到oneSubjectsList每个eduSubject对象
            EduSubject eduSubject = oneSubjectsList.get(i);
            //把eduSubject里面的值取出来，放在OneSubject对象里面
            // 把多个OneSubject放到finalSubjectList里面
            final OneSubject oneSubject = new OneSubject();
            /**
             * 方法一
             * oneSubject.setId(eduSubject.getId());
             * oneSubject.setTitle(eduSubject.getTitle());
             */
            /**
             * 方法二
             * @BeanUtils
             * */
            BeanUtils.copyProperties(eduSubject,oneSubject);

            finalSubjectList.add(oneSubject);

            //封装二级分类
            List<TwoSubject> finalTwoSubjectList = new ArrayList<>();
            //遍历二级分类list
            for (int m = 0; m < twoSubjectsList.size(); m++) {
                //获取每个二级分类
                EduSubject tSubject = twoSubjectsList.get(m);
                //判断二级分类parentid和一级分类id是否一样
                if(tSubject.getParentId().equals(eduSubject.getId())){
                    //把tSubject值复制到TwoSubject里面，放到twoFinalSubject
                    final TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    finalTwoSubjectList.add(twoSubject);
                }
            }
            //把一级下面所有二级分类放到一级分类里面
            oneSubject.setChildren(finalTwoSubjectList);
        }



        return finalSubjectList;
    }
}
